﻿using System;
using System.Data;
using System.Globalization;
using System.Web.UI.WebControls;
using Pantry.Business;
using Pantry.Commons;

namespace Pantry.Web.Users.Controls
{
    public partial class ListSupportingMaterial : System.Web.UI.UserControl
    {
        private readonly IFormatProvider _formatProvider = new CultureInfo("en-gb");
        private static bool _isSort = true;
        private DataView _dataView;
        private SupportingMaterialBus _supBus;

        protected void Page_Load(object sender, EventArgs e)
        {
            FillSupportingMaterialToGridview(CheckboxIsInActive.Checked);
        }

        /// <summary>
        /// Fills the Supporting Material to gridview.
        /// </summary>
        private void FillSupportingMaterialToGridview(bool isInActived)
        {   
            try
            {
                if (Request.QueryString["id"] != null)
                {
                    int orgId = int.Parse(Request.QueryString["id"]);
                    _supBus = new SupportingMaterialBus();
                    DataTable dataTable = _supBus.SelectAllSupportingMaterialByOrgId(orgId);
                    _dataView = dataTable.DefaultView;
                    if (!isInActived)
                    {
                        _dataView.RowFilter = "IsActive = 'Yes'";
                        _dataView.Sort = "Type DESC";
                    }

                    CollectionPager.PageSize = 15;
                    CollectionPager.BindToControl = RepeaterListSupportingMaterial;
                    CollectionPager.DataSource = _dataView;
                    RepeaterListSupportingMaterial.DataSource = CollectionPager.DataSourcePaged;
                    RepeaterListSupportingMaterial.DataBind();
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
                ExceptionHandler.WriteErrorLog(ex);
            }
        }

        /// <summary>
        /// Sorts the supporting material.
        /// </summary>
        /// <param name="field">The field.</param>
        private void SortSupportingMaterial(string field)
        {
            try
            {
                if (_isSort)
                {
                    _dataView.Sort = string.Format(_formatProvider, "{0} DESC", field);
                    _isSort = false;
                }
                else
                {
                    _dataView.Sort = string.Format(_formatProvider, "{0} ASC", field);
                    _isSort = true;
                }
            }
            catch (Exception exception)
            {
                ExceptionHandler.WriteErrorLog(exception);
            }
        }

        protected void SortUrlClick(object sender, EventArgs e)
        {
            SortSupportingMaterial("Url");
        }

        protected void SortDesClick(object sender, EventArgs e)
        {
            SortSupportingMaterial("Description");
        }

        protected void SortTypeClick(object sender, EventArgs e)
        {
            SortSupportingMaterial("Type");
        }

        protected void SortAddedByClick(object sender, EventArgs e)
        {
            SortSupportingMaterial("AddedBy");
        }

        protected void SortAddedDateClick(object sender, EventArgs e)
        {
            SortSupportingMaterial("AddedDate");
        }

        protected void SortIsActiveClick(object sender, EventArgs e)
        {
            SortSupportingMaterial("IsActive");
        }

        protected void CreateSupportingMaterialClick(object sender, EventArgs e)
        {
            string orgId = Request.QueryString["id"];
            Response.Redirect("AddSupportingMaterial.aspx?id="+orgId);
        }

        /// <summary>
        /// Marks the activated.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.RepeaterCommandEventArgs"/> instance containing the event data.</param>
        protected void MarkActivated(object source, System.Web.UI.WebControls.RepeaterCommandEventArgs e)
        {
            var hId = (HiddenField)RepeaterListSupportingMaterial.Items[e.Item.ItemIndex].FindControl("HiddenFieldMaterialId");
            string[] values = hId.Value.Split('$');
            if (e.CommandName == "update")
            {
                int id = int.Parse(values[1]);
                _supBus = new SupportingMaterialBus();
                _supBus.MarkActivated(true, id);
                FillSupportingMaterialToGridview(CheckboxIsInActive.Checked);
                Response.Redirect("EditSupportingMaterial.aspx?action=update&orgId=" + values[0] + "&materialId=" + values[1]);
            }
            else
            {
                Response.Redirect("EditSupportingMaterial.aspx?action=update&orgId=" + values[0] + "&materialId=" + values[1]);
            }
        }
    }
}